package com.scs.application.modules.base.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.modules.base.entity.PackTempletItem;
import com.scs.application.modules.base.service.PackTempletItemService;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 */
@RestController
@RequestMapping("/base/pack_templet_item")
public class PackTempletItemController extends CrudController<PackTempletItemService, PackTempletItem>{
    /**
     * 添加/更新数据
     */
    @PostMapping("/save")
    public RestResult saveItem(@Valid @RequestBody PackTempletItem entity) {
        if (entity.getPackageQty() == null || entity.getPackageQty() <=0) throw new BusinessException("包装数量不能小于0");

        if (entity.getFlagMain() != null && entity.getFlagMain()){
            //主耗材同一个包下面只能有一个
            PackTempletItem packTempletItem=service.getOne(Wrappers.<PackTempletItem>query().eq("pack_templet_id",entity.getPackTempletId()).eq("flag_main",1). ne("id",entity.getId()));
            if(packTempletItem != null) throw new BusinessException("主耗材同一个包下面只能有一个，该模板主耗材已被【"+packTempletItem.getMatrName()+"】设置");
        }
        boolean isOk = service.saveOrUpdate(entity);
        if (!isOk) {
            return RestResult.error("数据保存失败");
        }
        return RestResult.ok("数据保存成功", entity);
    }

    /**
     * @Description：批量添加
     */
    @PostMapping("/saveBatchUser")
    public RestResult saveBatchUser(@Valid @RequestBody List<PackTempletItem> entityList) {
        service.saveBatchUser(entityList);
        return RestResult.ok("批量添加成功",  entityList);
    }
}
